#pragma once
#include <sys/epoll.h>
#include <functional>

#include "inetaddr.hpp"
#include "log.hpp"

/*链接事件处理模块*/
using handle_t=std::function<bool(std::string&,std::string&)>;//事件处理方法
using socket_t=int;

class reactor;//前置声明

class connection{//基类链接
public:
    connection()
    {}

    void setevent(uint32_t event){_event=event;}
    void setfd(int fd) {_sockfd=fd;}
    void setaddr(inetaddr& peer) {_peer=peer;}
    void setown(reactor* reactor) {_own=reactor;}

    virtual void _recv()=0;//读取链接的数据
    virtual void _send()=0;//向链接发送数据
    virtual void _except()=0;//对于链接IO的异常处理

    int GetFd() const { return _sockfd;} 
    uint32_t GetEvent() const { return _event;}  
    void Close() {::close(_sockfd);}//关闭链接
    void RegistHandle(handle_t handle) {_execute=handle;}

    ~connection(){}
protected:
    int _sockfd;                //套接字fd
    uint32_t _event;            //对于该fd应该关注的事件
    std::string inbuffer;        //应用层输入缓冲区
    std::string outbuffer;      //应用层输出缓冲区
    reactor* _own;              //该链接所在的reactor
    inetaddr _peer;             //对端主机的信息
    handle_t _execute;

};

using connect_t=std::shared_ptr<connection>;

